'''
生成地图的脚本
'''
import os
import turtle
from PIL import Image


'''尺寸与颜色'''
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 1000
LINE_COLOR = (0, 0, 0)
LINE_WIDTH = 10
BACKGROUND_COLOR = (0, 0, 0)

'''路线坐标与颜色(左上角为坐标原点)'''
LINE_ONE_POINTS = [(550, 250), (550, 300), (500, 360), (500, 820), (670, 820)]
LINE_ONE_COLOR = (15, 15, 150)

LINE_TWO_POINTS = [(340, 255), (290, 300), (445, 455), (500, 455),(580, 530), (700, 530), (865, 700)]
LINE_TWO_COLOR = (255, 99, 61)

LINE_THREE_POINTS = [(875, 145), (670, 145), (670, 285), (560, 395), (560, 455),(520, 495), (520, 530), (420, 530), (385, 570), (215, 575), (135, 650)]
LINE_THREE_COLOR = (214, 15, 107)

LINE_FOUR_POINTS = [(140, 370), (140, 430), (530, 430), (600, 495), (875, 495)]
LINE_FOUR_COLOR = (27, 173, 99)

LINE_FIVE_POINTS = [(650, 140), (560, 140), (560, 240), (480, 240), (480, 350), (420, 420), (420, 530), (470, 595), (470, 765), (430, 810), (430, 840)]
LINE_FIVE_COLOR = (160, 63, 146)

LINE_SIX_POINTS = [(260, 180), (260, 285), (390, 285), (470, 370), (560, 370), (600, 410), (600, 470), (550, 515), (550, 835)]
LINE_SIX_COLOR = (190, 115, 49)

LINE_SEVEN_POINTS = [(430, 335), (605, 335), (675, 410), (680, 530), (610, 605), (425, 605), (345, 525), (350 ,410), (430, 335)]
LINE_SEVEN_COLOR = (101, 208, 222)

LINE_EIGHT_POINTS = [(865, 415), (675, 410), (620, 470), (620, 540), (585, 575), (450, 575), (320 ,700), (230, 700)]
LINE_EIGHT_COLOR = (195, 213, 31)

LINE_NINE_POINTS = [(315, 395), (315, 590), (370, 645), (595, 645)]
LINE_NINE_COLOR = (241, 173, 23)

"""绘制路线"""
def walk_line(coordinate_list, line_color):
    turtle.pencolor(line_color)  # 线条颜色
    turtle.penup()  # 抬起画笔
    turtle.goto(coordinate_list[0][0] - SCREEN_WIDTH/2, SCREEN_HEIGHT/2 - coordinate_list[0][1])  # 起点
    turtle.pendown()  # 放下画笔
    for coordinate in coordinate_list:
        x = coordinate[0] - SCREEN_WIDTH/2
        y = SCREEN_HEIGHT/2 - coordinate[1] # 坐标变换(turtle默认中心为原点)
        turtle.goto(x, y)

"""运行"""
def run():
    '''初始化'''
    turtle.setup(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0)  # 窗口大小、位置
    turtle.colormode(255)
    turtle.bgcolor(BACKGROUND_COLOR) # 背景颜色
    turtle.pensize(LINE_WIDTH)  # 线条宽度
    turtle.hideturtle() # 隐藏海龟

    '''绘制地图'''
    walk_line(LINE_ONE_POINTS, LINE_ONE_COLOR)  # 1号线
    walk_line(LINE_TWO_POINTS, LINE_TWO_COLOR)  # 2号线
    walk_line(LINE_THREE_POINTS, LINE_THREE_COLOR)  # 3号线
    walk_line(LINE_FOUR_POINTS, LINE_FOUR_COLOR)  # 4号线
    walk_line(LINE_FIVE_POINTS, LINE_FIVE_COLOR)  # 5号线
    walk_line(LINE_SIX_POINTS, LINE_SIX_COLOR)  # 6号线
    walk_line(LINE_SEVEN_POINTS, LINE_SEVEN_COLOR)  # 7号线
    walk_line(LINE_EIGHT_POINTS, LINE_EIGHT_COLOR)  # 8号线
    walk_line(LINE_NINE_POINTS, LINE_NINE_COLOR)  # 9号线

    '''将Turtle图形转换为PNG图像并保存'''
    turtle.getscreen().getcanvas().postscript(file="./image/map.eps", width=SCREEN_WIDTH, height=SCREEN_HEIGHT)
    # im = Image.open("./image/map.eps")
    # fig = im.convert('RGBA')
    # fig.save("map.png")

    # os.remove("./image/map.eps")

if __name__ == '__main__':
    run()